package com.example.administrator.weather;

import android.util.Log;

import com.example.administrator.weather.WeathJsonBean.*;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by Administrator on 2016/9/4.
 */
public class weatherModle {
    private List<String> list = new ArrayList<>();

    public List<String> getWeather(String str) {
        try {
            String path = "http://apicloud.mob.com/v1/weather/ip?key=143ba61bf5a2b&ip=" + str;
            Log.e("path", path);
            OkHttpClient httpClient = new OkHttpClient();
            Request request = new Request.Builder().url(path).get().build();
            Response response = httpClient.newCall(request).execute();
            String result1 = response.body().string();
            System.out.println(result1 + "\n返回的结果");
            Gson gson = new Gson();
            WeathJsonBean weathJsonBean = gson.fromJson(result1, WeathJsonBean.class);
            List<Result> result = weathJsonBean.getResult();
            String data = result.get(0).date;
            String weather = result.get(0).weather;
            String wind = result.get(0).wind;
            String temp = result.get(0).temperature;
            list.add(data);
            list.add(weather);
            list.add(wind);
            list.add(temp);
            List<Result.FutureBean> futureBeen = weathJsonBean.getResult().get(0).getFuture();
            for (int i = 0; i <futureBeen.size() ; i++) {
                String data1 = futureBeen.get(1).date;
                String weather1 = futureBeen.get(1).dayTime;
                String wind1 = futureBeen.get(1).wind;
                String temp1 = futureBeen.get(1).temperature;
                String data2 = futureBeen.get(2).date;
                String weather2 = futureBeen.get(2).dayTime;
                String wind2 = futureBeen.get(2).wind;
                String temp2 = futureBeen.get(2).temperature;
                list.add(data1);
                list.add(weather1);
                list.add(wind1);
                list.add(temp1);
                list.add(data2);
                list.add(weather2);
                list.add(wind2);
                list.add(temp2);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("list", list.toString());
        return list;
    }
}
